﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ini4Net;
using Microsoft.Win32;

namespace IniEditor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private Ini ini = new Ini();

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = ".ini";
            dlg.Filter = "Ini Documents (.ini)|*.ini";
            Nullable<bool> res = dlg.ShowDialog();
            if(res == true)
            {
                string fn = dlg.FileName;
                ini.Read(fn);
                UpdateTreeview();
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = ".ini";
            dlg.Filter = "Ini Documents (*.ini)|*.ini";
            Nullable<bool> res = dlg.ShowDialog();
            if(res == true)
            {
                string fn = dlg.FileName;
                ini.Save(fn);
            }
        }

        private bool inUpdateMode = false;

        private void UpdateTreeview()
        {
            inUpdateMode = true;

            // do we have a current selection?
            TreeViewItem currentItem = (treeView1.SelectedItem as TreeViewItem);
            
            treeView1.Items.Clear();
            txtIni.Clear();
            txtIni.Text = ini.ToString();

            foreach(IniSection s in ini)
            {
                TreeViewItem tvi = new TreeViewItem();
                tvi.Header = s.SectionName;
                tvi.Tag = "Section";
                tvi.IsExpanded = true;
                
                foreach(IniKey k in s)
                {
                    TreeViewItem n = new TreeViewItem();
                    n.Header = k.Key;
                    n.Tag = "Key";
                    n.IsExpanded = true;

                    if (currentItem != null)
                    {
                        n.IsSelected = n.Header == currentItem.Header;
                    }

                    tvi.Items.Add(n);
                }
                treeView1.Items.Add(tvi);
            }
            
            inUpdateMode = false;
        }

        private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (!inUpdateMode)
            {
                TreeViewItem tvi = (e.NewValue as TreeViewItem);
                TreeViewItem tvp = (tvi.Parent as TreeViewItem);

                if (tvi.Tag == "Key")
                {
                    lblSection.Content = string.Format("Section: {0}", tvp.Header);
                    textBox1.Text = tvi.Header.ToString();
                    textBox2.Text = ini[tvp.Header.ToString()][textBox1.Text].Value;
                }
            }
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            if (treeView1.SelectedItem != null)
            {
                string section = ((treeView1.SelectedItem as TreeViewItem).Parent as TreeViewItem).Header.ToString();
                ini[section].Set(textBox1.Text, textBox2.Text);
                UpdateTreeview();
            }
        }

    }
}
